<template>
    <div class="app-container">
        <h4 class="form-header h4">基本信息</h4>
        <el-form ref="form" :model="form" label-width="80px">
            <el-row>
                <el-col :span="8" :offset="2">
                    <el-form-item label="串口号" prop="serialCode">
                        <el-input v-model="form.serialCode" disabled />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <h4 class="form-header h4">命令列表</h4>
        <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table"
            @selection-change="handleSelectionChange" :data="commands.slice((pageNum - 1) * pageSize, pageNum * pageSize)">
            <el-table-column label="序号" type="index" align="center">
                <template slot-scope="scope">
                    <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
            <el-table-column label="命令名称" align="center" prop="name" />
            <el-table-column label="命令指令" align="center" prop="command" />
            <el-table-column label="命令类别" align="center" prop="commandType">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.command_type" :value="scope.row.commandType" />
                </template>
            </el-table-column>
            <el-table-column label="读取位置" width="120" align="center" prop="dataStartBit" />
            <el-table-column label="分割位数" width="120" align="center" prop="dataSplitLen" />
            <el-table-column label="数据位号" align="center" prop="dataBit" />
            <el-table-column label="对比值" width="120" align="center" prop="dataJudgment" />
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />

        <el-form label-width="100px">
            <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
                <el-button type="primary" @click="submitForm()">提交</el-button>
                <el-button @click="close()">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
    
<script>
import { getSerial,getListCommand, chooseCommand } from "@/api/device/serial";

export default {
    name: "AuthDataCheck",
    dicts: ['sys_normal_disable','command_type', 'data_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 分页信息
            total: 0,
            pageNum: 1,
            pageSize: 10,
            // 选中类型编号
            commandIds: [],
            // 类型信息
            commands: [],
            // 串口信息
            form: {},
            queryParams:{}
        };
    },
    created() {
        const serialId = this.$route.params && this.$route.params.serialId;
        if (serialId) {
            this.getList(serialId);
        }
    },
    methods: {
        getList(serialId) {
            this.loading = true;
            getSerial(serialId).then((response) => {
                this.form = response.data;
            });
            getListCommand(this.queryParams).then(response => {
                this.commands = response.rows;
                this.total = response.total;
            });
            this.loading = false;
        },
        /** 单击选中行数据 */
        clickRow(row) {
            this.$refs.table.toggleRowSelection(row);
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.commandIds = selection.map((item) => item.id);
        },
        // 保存选中的数据编号
        getRowKey(row) {
            return row.id;
        },
        /** 提交按钮 */
        submitForm() {
            const serialId = this.form.id;
            const commandIds = this.commandIds.join(",");
            chooseCommand({ serialId: serialId, commandIds: commandIds }).then((response) => {
                this.$modal.msgSuccess("开始下发");
                this.close();
            });
        },
        /** 关闭按钮 */
        close() {
            const obj = { path: "/device/serial" };
            this.$tab.closeOpenPage(obj);
        },
    },
};
</script>